package com.atgm.mall.gmmailproduct.service.impl;

import com.atgm.common.utils.GmMallUtils;
import com.atgm.mall.gmmailproduct.dao.AttrAttrgroupRelationDao;
import com.atgm.mall.gmmailproduct.dao.AttrDao;
import com.atgm.mall.gmmailproduct.dao.AttrGroupDao;
import com.atgm.mall.gmmailproduct.dao.GameAttrGroupRelaDao;
import com.atgm.mall.gmmailproduct.entity.AttrAttrgroupRelationEntity;
import com.atgm.mall.gmmailproduct.entity.AttrEntity;
import com.atgm.mall.gmmailproduct.entity.AttrGroupEntity;
import com.atgm.mall.gmmailproduct.entity.GameAttrGroupRelaEntity;
import com.atgm.mall.gmmailproduct.service.GameAttrGroupRelaService;
import com.atgm.mall.gmmailproduct.vo.resp.AttrGroupWithAttrVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GameAttrGroupRelaServiceImpl extends ServiceImpl<GameAttrGroupRelaDao, GameAttrGroupRelaEntity> implements GameAttrGroupRelaService {

    @Autowired
    private GameAttrGroupRelaDao gameAttrGroupRelaDao;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private AttrDao attrDao;

    @Override
    public List<AttrGroupWithAttrVo> queryGameAttr(Long gameId) {
        List<AttrGroupWithAttrVo> attrGroupWithAttrVos = new ArrayList<>();

        QueryWrapper<GameAttrGroupRelaEntity> gameAttrGroupRelaEntityQueryWrapper = new QueryWrapper<>();
        gameAttrGroupRelaEntityQueryWrapper.select("attr_group_id");
        gameAttrGroupRelaEntityQueryWrapper.eq("game_id", gameId);
        List<GameAttrGroupRelaEntity> gameAttrGroupRelaEntities = gameAttrGroupRelaDao.selectList(gameAttrGroupRelaEntityQueryWrapper);
        if (GmMallUtils.isEmpty(gameAttrGroupRelaEntities)) {
            return null;
        }
        List<Long> attrGroupIdList = gameAttrGroupRelaEntities.stream().map(GameAttrGroupRelaEntity::getAttrGroupId).collect(Collectors.toList());

        //获取属性分组名称
        QueryWrapper<AttrGroupEntity> attrGroupEntityQueryWrapper = new QueryWrapper<>();
        attrGroupEntityQueryWrapper.select("attr_group_id", "attr_group_name");
        attrGroupEntityQueryWrapper.in("attr_group_id", attrGroupIdList);
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(attrGroupEntityQueryWrapper);
        if (GmMallUtils.isEmpty(attrGroupEntities)) {
            return null;
        }

        //获取属性分组绑定的配置参数信息
        QueryWrapper<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityQueryWrapper = new QueryWrapper<>();
        attrAttrgroupRelationEntityQueryWrapper.select("attr_id", "attr_group_id");
        attrAttrgroupRelationEntityQueryWrapper.in("attr_group_id", attrGroupIdList);
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao.selectList(attrAttrgroupRelationEntityQueryWrapper);
        if (GmMallUtils.isEmpty(attrAttrgroupRelationEntities)) {
            return null;
        }

        List<Long> attrIdList = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

        QueryWrapper<AttrEntity> attrEntityQueryWrapper = new QueryWrapper<>();
        attrEntityQueryWrapper.select("attr_id", "attr_name");
        attrEntityQueryWrapper.in("attr_id", attrIdList);
        List<AttrEntity> attrEntities = attrDao.selectList(attrEntityQueryWrapper);
        if (GmMallUtils.isEmpty(attrEntities)) {
            return null;
        }

        attrGroupEntities.forEach(attrGroupEntity -> {
            AttrGroupWithAttrVo attrGroupWithAttrVo = new AttrGroupWithAttrVo();
            attrGroupWithAttrVo.setAttrGroupId(attrGroupEntity.getAttrGroupId());
            attrGroupWithAttrVo.setAttrGroupName(attrGroupEntity.getAttrGroupName());

            //获取属性分组绑定的配置参数信息
            List<Object> attrGroupBindAttrIdList = attrAttrgroupRelationEntities.stream().filter(attrAttrgroupRelationEntity -> {
                return attrGroupEntity.getAttrGroupId().equals(attrAttrgroupRelationEntity.getAttrGroupId());
            }).map(item -> {
                return item.getAttrId();
            }).collect(Collectors.toList());

            //获取属性值等信息，填充
            if (GmMallUtils.isNotEmpty(attrGroupBindAttrIdList)) {
                List<AttrEntity> attrEntitiesResult = attrEntities.stream().filter(attrEntity -> {
                    return attrGroupBindAttrIdList.contains(attrEntity.getAttrId());
                }).collect(Collectors.toList());
                attrGroupWithAttrVo.setAttrs(attrEntitiesResult);
            }
            attrGroupWithAttrVos.add(attrGroupWithAttrVo);
        });

        return attrGroupWithAttrVos;
    }
}
